package io.github.uicdb.gravity_generator.block;

import io.github.uicdb.gravity_generator.GGRegistries;
import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class BlockGravityGenerator extends Block implements EntityBlock {

    public BlockGravityGenerator(Properties properties) {
        super(properties);
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
        return GGRegistries.GRAVITY_GENERATOR_TYPE.get().create(pos,state);
    }

    @Override
    public void fallOn(@NotNull Level level, @NotNull BlockState state, @NotNull BlockPos blockPos, @NotNull Entity entity, float fallDistance) {
        super.fallOn(level, state, blockPos, entity, fallDistance);
        level.playSound(null,entity, SoundEvents.ANVIL_USE, SoundSource.BLOCKS,1,1);
        level.getBlockEntity(blockPos, GGRegistries.GRAVITY_GENERATOR_TYPE.get()).ifPresent(beGravityGenerator -> {
            beGravityGenerator.conversionEnergy(entity,fallDistance);
        });
    }


    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level, @NotNull BlockState blockState, @NotNull BlockEntityType<T> blockEntityType) {
        if(blockEntityType.equals(GGRegistries.GRAVITY_GENERATOR_TYPE.get())) {
            return (belevel, blockPos, state, be) ->((BEGravityGenerator)(be)).tick();
        }
        return null;
    }

}
